package com.akita.spring.ai.config;

import com.akita.spring.ai.constants.SystemConstants;
import com.akita.spring.ai.tools.CourseTools;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.OllamaEmbeddingModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

@Configuration
public class CommonConfiguration {

    @Bean
    public ChatMemory chatMemory() {
        // 聊天上下文需要被保存
        return new InMemoryChatMemory();
    }

    @Bean(name = "vectorStore")
    public VectorStore vectorStore(OpenAiEmbeddingModel embeddingModel) {
        // 进行知识库的相关配置
        return SimpleVectorStore.builder(embeddingModel).build();
    }

    @Bean(name = "ollamaChatClient")
    public ChatClient chatClient(OllamaChatModel ollamaChatModel, ChatMemory chatMemory) {
        // Ollama的聊天模型配置
        return ChatClient
                .builder(ollamaChatModel)
                .defaultSystem("你是一个热心、知识渊博的AI助手，你的名字叫李光洋，请你以李光洋的身份和语气回答问题。")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        new MessageChatMemoryAdvisor(chatMemory))
                .build();
    }

    @Bean(name = "aliyunChatClient")
    public ChatClient aliyunChatClient(OpenAiChatModel openAiChatModel, ChatMemory chatMemory) {
        return ChatClient.builder(openAiChatModel)
                .defaultOptions(ChatOptions.builder().model("deepseek-r1").build())
                .defaultSystem("你是一个热心、知识渊博的AI助手，你的名字叫李光洋，请你以李光洋的身份和语气回答问题。")
                .defaultAdvisors(new SimpleLoggerAdvisor())
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory))
                .build();
    }

    @Bean(name = "aliMultiModalChatClient")
    public ChatClient aliMultiModalChatClient(OpenAiChatModel openAiChatModel, ChatMemory chatMemory) {
        return ChatClient.builder(openAiChatModel)
                .defaultOptions(ChatOptions.builder().model("qwen-omni-turbo").build())
                .defaultSystem("你是一个热心、知识渊博的AI助手，你的名字叫李光洋，请你以李光洋的身份和语气回答问题。")
                .defaultAdvisors(new SimpleLoggerAdvisor())
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory))
                .build();
    }

    @Bean(name = "aliGameChatClient")
    public ChatClient aliGameChatClient(OpenAiChatModel openAiChatModel, ChatMemory chatMemory) {
        return ChatClient
                .builder(openAiChatModel)
                .defaultSystem(SystemConstants.GAME_SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        new MessageChatMemoryAdvisor(chatMemory))
                .build();
    }

    @Bean(name = "serviceChatClient")
    public ChatClient serviceChatClient(OpenAiChatModel openAiChatModel, ChatMemory chatMemory,
            CourseTools courseTools) {
        return ChatClient
                .builder(openAiChatModel)
                .defaultSystem(SystemConstants.SERVICE_SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        new MessageChatMemoryAdvisor(chatMemory))
                .defaultTools(courseTools)
                .build();
    }

    @Bean(name = "pdfChatClient")
    public ChatClient pdfChatClient(OpenAiChatModel openAiChatModel, ChatMemory chatMemory, VectorStore vectorStore) {
        return ChatClient
                .builder(openAiChatModel)
                .defaultSystem("请根据上下文回答问题，遇到上下文中没有的信息，不要随意编造。")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        new MessageChatMemoryAdvisor(chatMemory),
                        new QuestionAnswerAdvisor(
                                vectorStore,
                                SearchRequest.builder()
                                        .similarityThreshold(0.6)
                                        .topK(2)
                                        .build()
                        )
                )
                .build();
    }


    // 初始化线程池
    @Bean(name = "executorService")
    public ThreadPoolExecutor executorService() {
        return new ThreadPoolExecutor(
                10,                      // 核心线程数
                20,                     // 最大线程数
                60L,                    // 空闲线程存活时间
                TimeUnit.SECONDS,       // 时间单位
                new LinkedBlockingQueue<>(100),  // 任务队列
                new ThreadPoolExecutor.CallerRunsPolicy()  // 拒绝策略
        );
    }


}
